//**************************************************************************************************************
// FILE: Amicable.cpp
//**************************************************************************************************************
#include <iostream>
#include <cstring>
#include <sstream>
#include "Keith.hpp"
#include "Main.hpp"
#include "Thread.hpp"
#include "Types.hpp"

using namespace std;
//using std::cout;
//using std::string;
//using std::endl;

//==============================================================================================================
// Function Definitions
//==============================================================================================================

//--------------------------------------------------------------------------------------------------------------
// FindKeiths()
//
// Finds all Keith numbers from 10 up to and including pLimit. When a Keith number is found, do nothing.
//-----------   ---------------------------------------------------------------------------------------------------
void FindKeiths(ulong pLimit)
{
	if (GetVerbose()) cout << "FindKeiths() Begin" << endl;
	    for (ulong n = 10; n <= pLimit; n++) {
	        if (IsKeith(n))
	            ;
	    }
	    if (GetVerbose()) cout << "FindKeiths() End" << endl;
}

//--------------------------------------------------------------------------------------------------------------
// FindKeithsThread()
//
// The starting function for a thread which finds all Keith numbers from 10 up to and including pLimit.
//--------------------------------------------------------------------------------------------------------------
void *FindKeithThread(void *pState)
{
	// If running in verbose mode, then tell the user that the thread is beginning.
	    if (GetVerbose()) cout << "FindKeithThread() Begin" << endl;

	    // We know that pState is really a pointer to a ThreadState structure, so type cast it.
	    ThreadState *state = static_cast<ThreadState *>(pState);

	    // Find all the primes from 2 to state->mLimit.
	    FindKeiths(state->mLimit);

	    // The thread exit code is 0 (success).
	    state->mExitCode = 0;

	    // If running verbose mode, then tell the user that the thread is ending.
	    if (GetVerbose()) cout << "FindKeithThread() End" << endl;

	    // Exit (terminate) the thread and return pState as the return variable.
	    pthread_exit(pState);
}

//--------------------------------------------------------------------------------------------------------------
// IsKeith()
//
// Returns true if pNum is a Keith number.
//--------------------------------------------------------------------------------------------------------------
bool IsKeith(ulong pNum)
{
	// First, convert to a string so we can find the seed numbers;
	stringstream ss;
	ss << pNum;
	string numStr = ss.str();

	//int seedNum = 0;
        int length = numStr.length();
	int nums[length-1];
        for(int i = 0; i < numStr.length(); i++) {
            int n = numStr[i] - '0';
            nums[i] = n;
        }

	return keithRecurs(nums, pNum, length);  //Recursive function to find determine if its a Keith number
}

 bool keithRecurs(int pNums[], ulong pNum, int pLength)
  {
                int newNum = 0;
  		for (int i = 0; i < pLength; i++)
  			newNum += pNums[i];
  		if (newNum > pNum)
  			return false;
  		else if (newNum == pNum)
  			return true;
  		else {
                    for (int i = 1; i < pLength; i++)
                        pNums[i-1] = pNums[i];
                    pNums[pLength-1] = newNum;
                    return keithRecurs(pNums, pNum, pLength);
                    }
  }
 
